$(function() {

	// init date tables
	var alarmConfigListTable = $("#alarm_config_list").dataTable({
		"deferRender": true,
		"processing" : true,
		"serverSide": true,
		"ajax": {
			url: base_url + "/alarm/config/list",
			type:"post",
			data : function ( d ) {
				var obj = {};
				return obj;
			}
		},
		"searching": false,
		"ordering": false,
		//"scrollX": true,	// scroll x，close self-adaption
		"columns": [
			{
				"data": 'id',
				"visible" : false,
				"width":'10%'
			},
			{
				"data": 'alarmType',
				"visible" : true,
				"width":'15%',
				"render": function ( data, type, row ) {
					if (data == 1) {
						return I18n.alarm_config_type_1
					}else if (data == 2) {
						return I18n.alarm_config_type_2
					}else if (data == 3) {
						return I18n.alarm_config_type_3
					}  else {
						return I18n.alarm_config_type_4
					}
				}
			},
			{
				"data": 'alarmTitle',
				"visible" : true,
				"width":'10%'
			},
			{
				"data": 'alarmTos',
				"visible" : true,
				"width":'20%'
			},
			{
				"data": 'alarmInterval',
				"visible" : true,
				"width":'10%'
			},
			{
				"data": 'validStatus',
				"visible" : true,
				"width":'10%',
				"render": function ( data, type, row ) {
					if (data == 'Y') {
						return I18n.alarm_config_status_valid
					} else {
						return I18n.alarm_config_status_invalid
					}
				}
			},
			{
				"data": I18n.system_opt ,
				"width":'15%',
				"render": function ( data, type, row ) {
					return function(){
						// html
						tableData['key'+row.id] = row;
						var html = '<p id="'+ row.id +'" >'+
							'<button class="btn btn-warning btn-xs update" type="button">'+ I18n.system_opt_edit +'</button>  '+
							'<button class="btn btn-danger btn-xs delete" type="button">'+ I18n.system_opt_del +'</button>  '+
							'</p>';

						return html;
					};
				}
			}
		],
		"language" : {
			"sProcessing" : I18n.dataTable_sProcessing ,
			"sLengthMenu" : I18n.dataTable_sLengthMenu ,
			"sZeroRecords" : I18n.dataTable_sZeroRecords ,
			"sInfo" : I18n.dataTable_sInfo ,
			"sInfoEmpty" : I18n.dataTable_sInfoEmpty ,
			"sInfoFiltered" : I18n.dataTable_sInfoFiltered ,
			"sInfoPostFix" : "",
			"sSearch" : I18n.dataTable_sSearch ,
			"sUrl" : "",
			"sEmptyTable" : I18n.dataTable_sEmptyTable ,
			"sLoadingRecords" : I18n.dataTable_sLoadingRecords ,
			"sInfoThousands" : ",",
			"oPaginate" : {
				"sFirst" : I18n.dataTable_sFirst ,
				"sPrevious" : I18n.dataTable_sPrevious ,
				"sNext" : I18n.dataTable_sNext ,
				"sLast" : I18n.dataTable_sLast
			},
			"oAria" : {
				"sSortAscending" : I18n.dataTable_sSortAscending ,
				"sSortDescending" : I18n.dataTable_sSortDescending
			}
		}
	});

	// table data
	var tableData = {};

	// search btn
	$('#searchBtn').on('click', function(){
		alarmConfigListTable.fnDraw();
	});


	$("#alarm_config_list").on('click', '.delete',function() {
		var id = $(this).parent('p').attr("id");

		layer.confirm( I18n.system_ok + I18n.system_opt_del + '?', {
			icon: 3,
			title: I18n.system_tips ,
			btn: [ I18n.system_ok, I18n.system_cancel ]
		}, function(index){
			layer.close(index);

			$.ajax({
				type : 'POST',
				url : base_url + "/alarm/config/delete",
				data : {
					"id" : id
				},
				dataType : "json",
				success : function(data){
					if (data.code == 200) {
						layer.msg( I18n.system_success );
						alarmConfigListTable.fnDraw(false);
					} else {
						layer.msg( data.msg || I18n.system_opt_del + I18n.system_fail );
					}
				}
			});
		});
	});

	// add
	$(".add").click(function(){
		$('#addModal').modal({backdrop: false, keyboard: false}).modal('show');
	});
	var addModalValidate = $("#addModal .form").validate({
		errorElement : 'span',
		errorClass : 'help-block',
		focusInvalid : true,
		rules : {
			alarmTitle : {
				required : true,
				rangelength:[2, 100]
			},
			alarmTos : {
				required : true,
				rangelength:[2, 2000]
			}
		},
		messages : {
			alarmTitle : {
				required : I18n.system_please_input + I18n.alarm_config_title,
				rangelength: I18n.system_lengh_limit + "[2-100]"
			},
			alarmTos : {
				required : I18n.system_please_input + I18n.alarm_config_tos,
				rangelength: I18n.system_lengh_limit + "[2-2000]"
			}
		},
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error');
		},
		success : function(label) {
			label.closest('.form-group').removeClass('has-error');
			label.remove();
		},
		errorPlacement : function(error, element) {
			element.parent('div').append(error);
		},
		submitHandler : function(form) {
			var alarmInterval = $("#addModal .form input[name='alarmInterval']").val();
			if(!/^\d+$/.test(alarmInterval)) {
				alarmInterval = 0;
			}
			$("#addModal .form input[name='alarmInterval']").val(alarmInterval);

			var paramData = {
				"alarmType": $("#addModal .form input[name=alarmConfigType]:checked").val(),
				"alarmTitle": $("#addModal .form input[name=alarmTitle]").val(),
				"alarmTos": $("#addModal .form textarea[name=alarmTos]").val(),
				"alarmInterval": $("#addModal .form input[name=alarmInterval]").val(),
				"validStatus": $("#addModal .form input[name=validStatus]:checked").val()
			};

			$.post(base_url + "/alarm/config/add", paramData, function(data, status) {
				if (data.code == "200") {
					$('#addModal').modal('hide');

					layer.msg( I18n.system_add_suc );
					alarmConfigListTable.fnDraw();
				} else {
					layer.open({
						title: I18n.system_tips ,
						btn: [ I18n.system_ok ],
						content: (data.msg || I18n.system_add_fail),
						icon: '2'
					});
				}
			});
		}
	});
	$("#addModal").on('hide.bs.modal', function () {
		$("#addModal .form")[0].reset();
		addModalValidate.resetForm();
		$("#addModal .form .form-group").removeClass("has-error");
		$(".remote_panel").show();	// remote
	});

	// update
	$("#alarm_config_list").on('click', '.update',function() {

		var id = $(this).parent('p').attr("id");
		var row = tableData['key'+id];

		// base data
		$("#updateModal .form input[name='id']").val( row.id );
		$("#updateModal .form input[name='alarmConfigType'][value='"+ row.alarmType +"']").click();
		$("#updateModal .form input[name='alarmTitle']").val( row.alarmTitle );
		$("#updateModal .form textarea[name='alarmTos']").val( row.alarmTos );
		$("#updateModal .form input[name='alarmInterval']").val( row.alarmInterval );
		$("#updateModal .form input[name='validStatus'][value='"+ row.validStatus +"']").click();
		// show
		$('#updateModal').modal({backdrop: false, keyboard: false}).modal('show');
	});
	var updateModalValidate = $("#updateModal .form").validate({
		rules : {
			alarmTitle : {
				required : true,
				rangelength:[2, 100]
			},
			alarmTos : {
				required : false,
				rangelength:[2, 2000]
			}
		},
		messages : {
			alarmTitle : {
				required : I18n.system_please_input + I18n.alarm_config_title,
				rangelength: I18n.system_lengh_limit + "[2-100]"
			},
			alarmTos : {
				required : I18n.system_please_input + I18n.alarm_config_tos,
				rangelength: I18n.system_lengh_limit + "[2-2000]"
			}
		},
		errorElement : 'span',
		errorClass : 'help-block',
		focusInvalid : true,
		highlight : function(element) {
			$(element).closest('.form-group').addClass('has-error');
		},
		success : function(label) {
			label.closest('.form-group').removeClass('has-error');
			label.remove();
		},
		errorPlacement : function(error, element) {
			element.parent('div').append(error);
		},
		submitHandler : function(form) {
			var paramData = {
				"id": $("#updateModal .form input[name=id]").val(),
				"alarmType": $("#updateModal .form input[name=alarmConfigType]:checked").val(),
				"alarmTitle": $("#updateModal .form input[name=alarmTitle]").val(),
				"alarmTos": $("#updateModal .form textarea[name=alarmTos]").val(),
				"alarmInterval": $("#updateModal .form input[name=alarmInterval]").val(),
				"validStatus": $("#updateModal .form input[name=validStatus]:checked").val()
			};

			$.post(base_url + "/alarm/config/update", paramData, function(data, status) {
				if (data.code == "200") {
					$('#updateModal').modal('hide');

					layer.msg( I18n.system_update_suc );
					alarmConfigListTable.fnDraw();
				} else {
					layer.open({
						title: I18n.system_tips ,
						btn: [ I18n.system_ok ],
						content: (data.msg || I18n.system_update_fail),
						icon: '2'
					});
				}
			});
		}
	});
	$("#updateModal").on('hide.bs.modal', function () {
		$("#updateModal .form")[0].reset();
		updateModalValidate.resetForm();
		$("#updateModal .form .form-group").removeClass("has-error");
		$(".remote_panel").show();	// remote
	});

});
